package com.kulian.Signalling.distribution;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kulian.Signalling.common.annotation.Log;
import com.kulian.Signalling.common.enums.LogModuleEnum;
import com.kulian.Signalling.common.result.PageResult;
import com.kulian.Signalling.common.result.Result;
import com.kulian.Signalling.distribution.query.AppPageQuery;
import com.kulian.Signalling.distribution.service.AppService;
import com.kulian.Signalling.distribution.utils.PathManager;
import com.kulian.Signalling.distribution.vo.AppPageVO;
import com.kulian.Signalling.distribution.vo.AppViewModel;
import com.kulian.Signalling.system.mode.UserPageVO;
import com.kulian.Signalling.system.mode.query.UserPageQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "03.分发中心")
@RestController
@RequestMapping("/api/v1/distribution")
public class AppController {

    @Resource
    private AppService appService;
    @Resource
    private PathManager pathManager;

    @Operation(summary = "获取app列表")
    @GetMapping("/apps")
    public String apps(HttpServletRequest request) {
        try {
            List<AppViewModel> apps = this.appService.findAll();
            request.setAttribute("apps", apps);
            request.setAttribute("baseURL", this.pathManager.getBaseURL(false));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "index";
    }

    @Operation(summary = "App分页列表")
    @GetMapping("/appList")
    @Log(value = "App分页列表", module = LogModuleEnum.USER)
    public PageResult<AppPageVO> getUserPage(
            @Valid AppPageQuery queryParams
    ) {
        IPage<AppPageVO> result = appService.getAppPage(queryParams);
        return PageResult.success(result);
    }

    @GetMapping("/apps/{appID}")
    public String getAppById(@PathVariable("appID") String appID, HttpServletRequest request) {
        AppViewModel appViewModel = this.appService.getById(appID);
        request.setAttribute("package", appViewModel);
        request.setAttribute("apps", appViewModel.getPackageList());
        return "list";
    }

    @RequestMapping("/packageList/{appID}")
    @ResponseBody
    public Map<String, Object> getAppPackageList(@PathVariable("appID") String appID) {
        AppViewModel appViewModel = this.appService.getById(appID);
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("packages", appViewModel.getPackageList());
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
        }
        return map;
    }
    @Operation(summary = "删除app")
    @PostMapping("/app/delete")
    public Result deleteById(@Parameter(description = "应用id", example = "") @RequestParam String appId) {
        Map<String, Object> map = new HashMap<>();
        try {
            this.appService.deleteById(appId);
            map.put("success", true);//ff80808196e144360196e1446bbc0000
        } catch (Exception e) {
            map.put("success", false);
        }
        return Result.success(map);
    }

}
